"""5.3 【资源类】Resource Template 服务端开发"""

from mcp.server.fastmcp import FastMCP
# 创建MCP实例
mcp = FastMCP("Resource Template MCP Server")


#创建资源模板
@mcp.resource(
    uri="file:///query/{book_id}",
    name="book",
    description="查询书籍信息",
    mime_type="text/plain",
)
def query_book(book_id:str):
    """
    查询书籍信息
    Args:
        book_id: 书籍ID
    Returns: 返回书籍信息
    """
    #构建5条书信息，book_id,name,author,push_date
    books = [
        {"book_id":"1001","name":"Python编程","author":"Guido van Rossum","push_date":"2021-01-01"},
        {"book_id":"1002","name":"Java编程","author":"James Gosling","push_date":"2021-01-02"},
        {"book_id":"1003","name":"C++编程","author":"Bjarne Stroustrup","push_date":"2021-01-03"},
        {"book_id":"1004","name":"C#编程","author":"Anders Hejlsberg","push_date":"2021-01-04"},
        {"book_id":"1005","name":"JavaScript编程","author":"Brendan Eich","push_date":"2021-01-05"},
    ]
    #根据book_id查询书籍信息
    for book in books:
        if book["book_id"] == book_id:
            return book
if __name__ == "__main__":
    print("Resource Template MCP Server is running...")
    mcp.run(transport="sse")